package com.jxs.android.ges;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.jxs.android.util.StartHelper;
import com.jxs.android.weiget.TopBar;

public class FeatureGuide extends AbstractActivity implements OnClickListener {

	private ViewPager viewPager;
	private ArrayList<View> pageViews;
	private ViewGroup group;
	private ImageView[] guidePages;

	private Integer pageViewImages[] = new Integer[] { R.drawable.share_album_bg_1, R.drawable.share_album_bg_2, R.drawable.share_album_bg_3, };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_guide_view_main);
		initPageViews();
		guidePages = new ImageView[pageViews.size()];
		group = (ViewGroup) findViewById(R.id.viewGroup);
		viewPager = (ViewPager) findViewById(R.id.guidePages);
		for (int i = 0; i < pageViews.size(); i++) {
			ImageView imgView = new ImageView(FeatureGuide.this);
			imgView.setLayoutParams(new LayoutParams(20, 20));
			imgView.setPadding(20, 0, 20, 0);
			guidePages[i] = imgView;
			if (i == 0) {// 默认选中第一张图片
				guidePages[i].setBackgroundResource(R.drawable.page_indicator_focused);
			} else {
				guidePages[i].setBackgroundResource(R.drawable.page_indicator);
			}
			group.addView(guidePages[i]);
		}

		viewPager.setAdapter(new GuidePageAdapter());
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		if (null != getIntent() && null != getIntent().getExtras() && getIntent().getExtras().getBoolean("hinde", false)) {
			topBar.hindSelf();
		}
		topBarReturn(topBar);
		topBar.setTitle(R.string.help_title_label);
		topBar.hindSpinnerBtn();
	}

	private void initPageViews() {
		pageViews = new ArrayList<View>();
		for (int resId : pageViewImages) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			imageView.setBackgroundResource(resId);
			pageViews.add(imageView);
		}
	}

	/** 指引页面Adapter */
	private class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(pageViews.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int positon) {
			View itemView = pageViews.get(positon);
			((ViewPager) arg0).addView(itemView);
			if (positon == pageViews.size() - 1) {
				itemView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (null != getIntent() && null != getIntent().getExtras() && getIntent().getExtras().getBoolean("hinde", false)) {
							StartHelper.startActivity(LaucherMain.class);
						}
						finish();
					}
				});
			}
			return pageViews.get(positon);
		}

	}

	@Override
	public void onClick(View paramView) {

	}

	/** 指引页面改监听器 */
	private class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			for (int i = 0; i < guidePages.length; i++) {
				guidePages[arg0].setBackgroundResource(R.drawable.page_indicator_focused);
				if (arg0 != i) {
					guidePages[i].setBackgroundResource(R.drawable.page_indicator);
				}
			}

		}

	}

}